package com.jackiechancinema.ssm.service.Impl;

import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.AdminDao;
import com.jackiechancinema.ssm.entity.Admin;
import com.jackiechancinema.ssm.service.Inte.LoginService;
import com.jackiechancinema.ssm.utils.EncryptUtil;
import com.jackiechancinema.ssm.vo.R;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Objects;

@Data
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    AdminDao adminDao;
    @Override
    public R login(String name, String pass, HttpSession session) {
        Admin admin = adminDao.selectByName(name);
        if(admin != null){
            if(admin.getAdmin_flag() == SystemContant.ACCOUNT_FLAG_OK){
                if(Objects.equals(admin.getAdmin_password(), EncryptUtil.aesenc(SystemContant.PASS_KEY,pass))){
                    session.setAttribute("curruser",admin);
                    return R.ok(admin);
                }
                else{
                    return R.fail("密码或账号错误");
                }
            }else{
                return R.fail("账号不可用");
            }
        }else {
            return R.fail("账号不存在");
        }
    }

}
